package com.nytd.demo;

import com.nytd.demo.controller.UserController;
import com.nytd.demo.model.User;
import com.nytd.demo.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringIocApplication {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);

//		UserController userController = context.getBean(UserController.class);
//		UserController userController1 = (UserController) context.getBean("userController");
//		UserController userController2 = context.getBean("userController", UserController.class);

//		userController.hello();
//		userController1.hello();
//		userController2.hello();

//		System.out.println(userController);
//		System.out.println(userController1);
//		System.out.println(userController2);

//		UserService userService = context.getBean(UserService.class);
//		userService.sayHi("张三");
//		UserService userService1 = (UserService) context.getBean("aaa");
//		userService1.sayHi("李四");

		//@Service("aaa") 覆盖了默认名称,导致 "userService" 失效
		//覆盖了 Spring 默认的 Bean 命名规则,默认名称是类名首字母小写，如 userServiceImpl
		//此时 Bean 的唯一名称是 "aaa",而 "userService" 不再有效
//		UserService userService2 = (UserService) context.getBean("userService");
//		userService2.sayHi("王五");

//		UserRepository userRepository = context.getBean(UserRepository.class);
//		userRepository.sayHi("李四");

//		UserComponent userComponent = (UserComponent) context.getBean("userComponent");
//		userComponent.sayHi("王五");

//		UserConfiguration userConfiguration = context.getBean("userConfiguration", UserConfiguration.class);
//		userComponent.sayHi("老刘");

//		User user = context.getBean(User.class);
//		System.out.println(user);
//		User user1 = (User) context.getBean("user1");
//		System.out.println(user1);
//		User user2 = context.getBean("user2", User.class);
//		System.out.println(user2);
//		User user = (User)context.getBean("user2");
//		System.out.println(user);

		UserController userController = (UserController) context.getBean("userController");
		userController.hello();

	}


}
